package com.xiaode.interceptor;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.FileRender;
import com.xiaode.common.constant.Const;
import com.xiaode.exce.EnumError;

import java.io.FileWriter;
import java.io.IOException;


public class SysInterceptor implements Interceptor {
	
    @Override
    public void intercept(Invocation inv) {
        //用来服务之间调用 验证服务的密钥, 密钥通过才放行
        Controller c = inv.getController();
        String accessKey = c.getHeader("accessKey");
        String secretKey = c.getHeader("secretKey");
        String serviceId = c.getHeader("serviceId");
        
        System.out.println(accessKey);
        
        if(!StrUtil.isAllNotBlank(accessKey, secretKey, serviceId)) {
        	c.renderJson(Ret.fail().set("error", EnumError.NOT_AVAILABLE.getObj()));
            return;
        }
        
        String salt = Const.serverConst.serviceSalt;
        
    	byte[] key = (secretKey + salt).getBytes();
    	HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
    	String macHex1 = mac.digestHex(accessKey);
    	
    	String serviceSecretKey = Redis.use().get(Const.serverConst.serviceSecretKey + serviceId);
    	
    	System.out.println(serviceSecretKey);
    	if(!StrUtil.equals(macHex1, serviceSecretKey)) {
    		c.renderJson(Ret.fail().set("error", EnumError.NOT_AVAILABLE.getObj()));
            return;
    	}
    	
        inv.invoke();
    }
    
    
    
    
    public static void main(String[] args) throws IOException {
    	
    	String capitalize = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	String accessKey = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER + capitalize, 20);
        String secretKey = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER + capitalize, 30);
        String serviceId = StrUtil.uuid();

        String salt = Const.serverConst.serviceSalt;

    	// 此处密钥如果有非ASCII字符，考虑编码
    	byte[] key = (secretKey + salt).getBytes();
    	HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
    	String macHex1 = mac.digestHex(accessKey);

    	String template = "{serviceId: {} ,accessKey: {} ,secretKey: {} ,dense: {}}";
    	String str = StrUtil.format(template, serviceId, accessKey, secretKey, macHex1);

    	try (FileWriter writer = new FileWriter(PathKit.getWebRootPath() + "/serviceKey.txt", true)) {//只需要在后面添加true 就代表是append追加了
			writer.append(str + StrUtil.LF);
			writer.flush();
		}
    	Prop p = PropKit.useFirstFound("config-dev.txt");
    	RedisPlugin redisPlugin = new RedisPlugin("redis", p.get("redisUrl"), p.getInt("redisPort"), 5000, null, p.getInt("database"));
    	redisPlugin.start();
    	Redis.use().set(Const.serverConst.serviceSecretKey + serviceId, macHex1);

		
	}

	

}
